package org.lsms.servlet;

/**
 * servlet used to invite pc member
 * @author ZhanXueliang
 * 2010.8.29
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lsms.bean.User;
import org.lsms.dao.impl.DaoImpl;
import org.lsms.mail.MailSenderInfo;
import org.lsms.mail.SimpleMailSender;

public class invite_member extends HttpServlet {

	
	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		DaoImpl impl = new DaoImpl();
		
		HttpSession session = request.getSession();
		//int member_id = Integer.parseInt(session.getAttribute("userid").toString());
	
		String loginname = request.getParameter("loginname");
		String to = request.getParameter("email");
		String firstname = request.getParameter("firstname");
		String middlename = request.getParameter("middlename");
		String lastname = request.getParameter("lastname");
		
		User user = new User();
		user.setLoginname(loginname);
		user.setFirstname(firstname);
		user.setEmail(to);
		user.setMiddlename(middlename);
		user.setLastname(lastname);
		user.setPassword("123456");
		user.setPermission(1);
		user.setLogintimes(0);
		
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.init(mailInfo);
		mailInfo.setToAddress(to);
		mailInfo.setSubject("LSMS & ICSEE 2010 PC Member Invite E-Mail");
		
		String content = "Dear Mr/Mrs " + firstname+" "+middlename+" "+lastname+'\n'+'\n';
		String letter_content = impl.get_mail_content(2);
		content +=letter_content;
		String ps = "\nPS:\nYour Loginname:"+loginname+"  \nYour Password:123456";
		content +=ps;
		mailInfo.setContent(content);
		
		SimpleMailSender sms = new SimpleMailSender();
		
		if(impl.check_login_name(loginname)){
			if(impl.signup(user) && sms.sendTextMail(mailInfo)){
				String chair_info = "The invite email has been sent to "+to;
				response.sendRedirect("chair/chair_show_info.jsp?chair_info="+chair_info);	
			}else{
				String chair_info = "Problem occurs when inviting PC member.\n" +
						"Please try again later.";
				response.sendRedirect("chair/chair_show_info.jsp?chair_info="+chair_info);	
			}
		}else{
			String chair_info = "The login name "+loginname+" has been used!\n" +
					"Please choose anothor one";
			response.sendRedirect("chair/chair_show_info.jsp?chair_info="+chair_info);	
		}
		
	}

	
	public void init() throws ServletException {
		
	}

}
